package com.ciaojian.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ciaojian.core.model.CouponGrantRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 券码授予记录 Mapper 接口
 * </p>
 *
 * @author Atlakyn
 * @since 2021-08-03
 */
public interface CouponGrantRecordMapper extends BaseMapper<CouponGrantRecord> {
    /**
     * 查找用户特定券的送券人(限第一个送券人)
     *
     * @param target   用户
     * @param couponId 指定券
     * @return 送券人
     */
    @Select("SELECT source " +
            "FROM d_coupon_grant_record AS dcgr " +
            "         LEFT JOIN d_coupon_code AS dcc ON dcc.code = dcgr.code " +
            "WHERE target = #{target}  AND dcc.coupon_id = #{couponId} " +
            "order by dcgr.add_time " +
            "limit 1 ")
    Integer getSource(@Param("target") Integer target, @Param("couponId") Integer couponId);
}
